<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;

class jsamRequest extends jsamMain{

	public function caller($call){

		## Request
		if($call == 'request'){
			return $this->request();
		}
		
		## Sam text strings
		if($call == 'samtxt'){
			return $this->samtxt();
		}

		## Add request text by string
		if($call == 'addmsg'){
			return $this->addReqmsg();
		}
	}

	protected function addReqmsg(){

		$name_client = JRequest::getVar('clientName');
		$msg_client = JRequest::getVar('clientMsg');
		$reqID = JRequest::getVar('reqid');

		if($name_client != '' and $msg_client != '' and $reqID != ''){
			
			## Build database work
			$sdb = new jsamSamdb();
			$data = 0;
			$query = "	UPDATE requestlist 
						SET name = '".$name_client."', msg = '".$msg_client."'
						WHERE ID = '".$reqID."'
					";
			if($sql = $sdb->query($query)){
				$data = $sdb->affected_rows;
			}
			else{
				$data = 666;
			}
			
			if($data <= 0){
				$result = 'noeffect';
				$sdb->close();
			}
			else if($data == 666){
				$result = 'mysqlerror';
			}
			else if($data == 1){
				$result = 'done';
				$sdb->close();
			}
			else{
				$result = $data;
			}
		}
		else{
			
			$result = 'noParams';
		}
		return $result;
	}

	## Sam text Returner
	protected function samtxt(){

		$code = JRequest::getVar('jsamInfo');
		$data = $this->txtstrings($code);
		if($data != ''){
			$string = $data['txtstring'];
			$result = htmlspecialchars($string, ENT_QUOTES);
		}
		else{
			$result = JText::_('COM_JOOSAM_COM_REQUEST_NO_STRING_FOUND').$code;
		}
		return $result;
	}

	## Request Returner
	protected function request(){

		$predata = $this->preRequest();
		$conf = $this->requestconf();
		$usermsg = $conf['reqmsg'];
		$data = $predata['code'];
		$xtra = $predata['extra'];
		## Allow user text string check
		if($usermsg == 'true'){
			$text = 'allowed';
		}
		else{
			$text = 'blocked';
		}
		
		## Outputs
		## Error joosam event category check
		if($data == 'code001'){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_ERROR_CATCHECK'),
						'text' => $text,
						'xtra' => $xtra );
		}

		## We don't accapt requests of that category
		else if($data == 'code002'){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_NO_ACCAPT_CAT'),
						'text' => $text,
						'xtra' => $xtra );
		}

		## Invaled or no song id
		else if($data == 'code100'){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_INVALED_SONGID'),
						'text' => $text,
						'xtra' => $xtra );
		}

		## Not possible to connect to SAM server
		else if($data == 'code101'){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_NO_SAMCONNECT'),
						'text' => $text,
						'xtra' => $xtra );
		}

		## Wrong sam url
		else if($data == 'code102'){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_SAM_BAD_SUBURL'),
						'text' => $text,
						'xtra' => $xtra );
		}
		
		## php curl function is not active
		else if($data == 'code103'){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_CURL_OFFLINE'),
						'text' => $text,
						'xtra' => $xtra );
		}

		## Sam success request
		else if($data == 200){
			$result = array(
						'type' => 'success',
						'info' => $data,
						'text' => $text,
						'xtra' => $xtra );
		}

		## Unknow error
		else if($data == ''){
			$result = array(
						'type' => 'error',
						'info' => JText::_('COM_JOOSAM_COM_REQUEST_UNKNOWN_ERROR'),
						'text' => $text,
						'xtra' => $xtra );
		}

		## Sam returned a error code (only not 200)
		else{
			$result = array(
						'type' => 'warning',
						'info' => $data,
						'text' => $text,
						'xtra' => $xtra );
		}

		return $result;
	}

	## do request.
	protected function preRequest(){

		$songID = JRequest::getVar('spcid');
		$revt = $this->checkCatRequest($songID);
		
		$data = '';
		
		## Error connecting DB for category check
		if($revt == 'code03'){
			$data = array(
						'code' => 'code001',
						'extra' => 'noinfo' );
		}

		## Event category check don't allow request
		if($revt == 'code02'){
			$data = array(
						'code' => 'code002',
						'extra' => 'noinfo' );
		}

		##call request
		if($revt == 'code01'){
			$data = $this->doRequest($songID);
		}

		return $data;
	}

	protected function checkCatRequest($sid){

		$evt = $this->events();

		$catid = $evt['sprequests'];
		$songid = $sid;

		## Build database work
		$sdb = new jsamSamdb();
		$data = '';
		$query = "	SELECT *
					FROM categorylist
					WHERE songID = '".$songid."'
						AND categoryID IN (".$catid.")
					ORDER BY ID ASC
				";
		if($sql = $sdb->query($query)){
			while($row = $sql->fetch_assoc()){
				$data[] = $row;
			} $sql->free();
			$sdb->close();
			if($data != ''){
				return 'code01';				#############################################
			}									#											#
			else{								# code01 = request allowed					#
				return 'code02';				# code02 = request not while this event		#
			}									# code03 = error database connect;			#
		}										#											#
		else{									#############################################
			return 'code03';
		}
	}

	protected function doRequest($sid){

		$conf = $this->settings();
		$ip_client = getenv('REMOTE_ADDR');
		$ip_joomla = getenv("SERVER_ADDR");
		$ip_samSrv = $conf['samsrvip'];
		$port_sam = $conf['samsrvport'];
		$songID = $sid;
		$result = '';
		$xmldata = '';
		
		## Song ID Check
		settype($songID, 'integer');
		if(empty($songID)){
			$result = array(
				'code' => 'code100',
				'extra' => 'noinfo' );
		}

		## Go for request
		else{
			$sam = array(
				'url' => 'http://'.$ip_samSrv.':'.$port_sam.'/req/',
				'songid' => $songID,
				'client' => urlencode($ip_client) );

			$url = $sam['url'].'?songID='.$sam['songid'].'&host='.$sam['client'];

			## When curl can be used
			if(function_exists('curl_init')){
				$srv = curl_init($url);
				curl_setopt($srv, CURLOPT_HEADER, false);
				curl_setopt($srv, CURLOPT_RETURNTRANSFER, true);
				$srvData = curl_exec($srv);
				curl_close($srv);
				if($srvData == ''){
					$result = array(
								'code' => 'code101',
								'extra' => 'noinfo' );
				}
				else if($srvData == 'File not found'){
					$result = array(
								'code' => 'code102',
								'extra' => 'noinfo' );
				}
				else{
					$xml = simplexml_load_string($srvData);
					$msgCode = $xml->status->code;
					$msgReId = $xml->status->requestID;
					settype($msgCode, 'integer');
					settype($msgReId, 'integer');
					$result = array(
								'code' => $msgCode,
								'extra' => $msgReId );
				}
			}
			else{
				$result = array(
							'code' => 'code103',
							'extra' => 'noinfo' );
			}
		}
		return $result;
	}
}